#include <stdio.h>
#include <unistd.h>     
#include <errno.h>      
#include <stdlib.h>     
#include <pthread.h>    
#include <string.h>     

#define err() do { perror("thread6"); exit(EXIT_FAILURE); } while(0)

pthread_mutex_t mutex;
char array[26];

void * invert( void * args)
{
	int i;
	for(;;)
	{
		if(pthread_mutex_lock(&mutex)) err();
		for(i = 0; i < 26; ++i) array[i] ^= 32;
		if(pthread_mutex_unlock(&mutex)) err();
		usleep(300000);
	}
	return NULL;
}

void * reverse( void * args)
{
	char tmp;
	int i;
	for(;;)
	{
		if(pthread_mutex_lock(&mutex)) err();
		for(i = 0; i < 13; ++i)
		{
			tmp = array[i];
			array[i] = array[25 - i];
			array[25 - i] = tmp;
		}
		if(pthread_mutex_unlock(&mutex)) err();
		usleep(300000);
	}
	return NULL;
}

int main(int argc, char *argv[])
{
	int i;
	pthread_t thread_a,thread_b;
	if(pthread_mutex_init(&mutex, NULL) != 0) err();
	for(i = 0; i < 26; ++i) array[i] = 'a' + i;
	if(pthread_create (&thread_a, NULL,&invert, (void *) NULL) != 0) err();
        if(pthread_create (&thread_b, NULL,&reverse, (void *) NULL) != 0) err();
	for(;;)
	{
		if(pthread_mutex_lock(&mutex)) err();
		for(i = 0; i < 26; ++i) putchar(array[i]);
		putchar('\n');
		if(pthread_mutex_unlock(&mutex)) err();
		usleep(300000);
	}
	return 0;
}

